package com.ld.vaadin.view;

import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.login.LoginI18n;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@PageTitle("注册页面")
@Route(value = "register")
@AnonymousAllowed
public class RegisterView extends VerticalLayout {

    private final LoginForm loginForm;

    public RegisterView() {
        loginForm = new LoginForm();

        setSizeFull();
//        水平居中
        setAlignItems(Alignment.CENTER);
//        没有作用
//        setHorizontalComponentAlignment(Alignment.CENTER);
//        垂直居中
        setJustifyContentMode(JustifyContentMode.CENTER);

        getRegisterForm();
//        loginForm.addLoginListener(this::register);
//        loginForm.setAction("/api/v1/auth/register");
        add(loginForm);
    }

    private void getRegisterForm() {

        LoginI18n loginI18n = getLoginI18n();

//        loginForm.getElement().setAttribute("enctype", "application/json");
//        loginForm.getElement().setAttribute("content-type", "application/json;charset=UTF-8");
        loginForm.setI18n(loginI18n);
        loginForm.setForgotPasswordButtonVisible(false);
        loginForm.addLoginListener(loginEvent -> {
//            String attribute = this.getElement().getAttribute("enctype");
//            String attribute2 = this.getElement().getAttribute("content-type");
//            String enctype = this.getElement().getProperty("enctype");
//            String property = this.getElement().getProperty("content-type");
//            System.out.println(attribute);
//            loginEvent.
        });
        loginForm.addForgotPasswordListener(forgotPasswordEvent -> getUI().ifPresent(ui -> ui.navigate("/")));
    }

    private LoginI18n getLoginI18n() {
        LoginI18n loginI18n = new LoginI18n();

        LoginI18n.Form form = new LoginI18n.Form();
        form.setTitle("注册");
        form.setUsername("请输入账号");
        form.setPassword("请输入密码");
        form.setSubmit("注册");
        form.setForgotPassword("已有账号？点击登录");
        loginI18n.setForm(form);

        LoginI18n.ErrorMessage errorMessage = new LoginI18n.ErrorMessage();
        errorMessage.setUsername("账号不能为空");
        errorMessage.setPassword("密码不能为空");
        errorMessage.setMessage("");
        loginI18n.setErrorMessage(errorMessage);
        return loginI18n;
    }

//    private void register(AbstractLogin.LoginEvent loginEvent) {
//        String username = loginEvent.getUsername();
//        String password = loginEvent.getPassword();
//
//        var request = RegisterRequest.builder()
//                .email(username)
//                .credentials(password)
//                .build();
//        try {
//            authenticationService.register(request);
////            String token = response.getToken();
////            WebStorage.setItem("Authorization", "bearer " + token);
//
////        跳转登录页面
//            getUI().ifPresent(ui -> ui.navigate("/"));
//        } catch (Exception e) {
////            throw new RuntimeException(e);
//            Dialog dialog = new Dialog();
//            dialog.add("账号已经存在");
//            dialog.addOpenedChangeListener(openedChangeEvent -> loginForm.setEnabled(true));
//            dialog.open();
//        }
//    }

}
